<?php
namespace  zhtfrontend\components\services\goods;

use common\components\helpers\Fun;
use doumiaoduo\buildform\BuildForm;
use frontend\models\datas\GoodsClassTime;
use zhtfrontend\components\helpers\Funs;
use zhtfrontend\components\services\classmanager\Createclassmanage;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassManageAttribute;
use zhtfrontend\models\datas\ClassManageGoods;
use zhtfrontend\models\datas\ClassManageTeacher;
use zhtfrontend\models\datas\ClassManageTime;
use zhtfrontend\models\datas\Classroom;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsAttr;
use zhtfrontend\models\datas\GoodsTeacher;
use zhtfrontend\models\datas\Mechanic;
use yii\base\Action;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use zhtfrontend\models\datas\ReportRecord;
use yii\widgets\ActiveForm;
use yii\web\Response;

class AuditAction extends Action{

    public $type;

    public function run(){
        $this->controller->layout='@app/views/layouts/iframe.php';
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('frontend', '错误请求'));
        }
        $this->type= \Yii::$app->request->get('type_id');
        $goods = Goods::findOne(\Yii::$app->request->get('id'));
        $attr = Funs::getGoodsAttr($goods->id);

        $model = new ReportRecord();
        $model->scenario = 'audit';
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post())&& $model->validate()) {
            $model->module_id = $goods->id;
            $model->sender_id = \Yii::$app->user->id;
            $is_shelve = 0;
            $is_update_price = 1;
            $report = ReportRecord::find()->where(['module_id'=>$goods->id,'stage'=>1,'type'=>1])->one();
            if(\Yii::$app->user->identity->role == 'mec_principal'){//机构负责人操作
                if($model->status == 1){
                    $model->sendee_id = $report->sender_id; //教务
                    $status = 3;
                }else{
                    $model->sendee_id = $report->sender_id;
                    $status = 0;
                }
                $stage = 2;
                $model->mechanicremark = $model->remark;
                $model->operator_type = 'mechanic';
            }else if(\Yii::$app->user->identity->role == 'educational_administration'){//教务操作
                $finance = Admin::find()->where(['role'=>'finance','status'=>1])->one();
                if($model->status == 1){
                    $model->sendee_id = isset($finance->id)?$finance->id:0;//接收人财务
                    $status = 4;
                }else{
                    $model->sendee_id = $report->sender_id;
                    $status = 0;
                }
                $stage = 3;
                $model->workerremark = $model->remark;
                $model->operator_type = 'square';
            }else{ //财务操作
                if($model->status == 1){
                    $model->sendee_id = $report->sender_id; //接收人教务
                    $status = 1;
                    $is_shelve = 1;
                    $is_update_price = 0;
                }else{
                    $model->sendee_id = $report->sender_id;
                    $status = 0;
                }
                $stage = 4;
                $model->workerremark = $model->remark;
                $model->operator_type = 'square';
            }
            $model->stage =$stage;
            $model->type = 1;
            if ($model->save(false)) {
                $goods->is_shelve = $is_shelve;
                $goods->status = $status;
                $goods->is_update_price = $is_update_price;
                $goods->save(false);
                if($is_shelve == 1){ //生成班级
                   $classGoods = ClassManageGoods::find()->where(['goods_id'=>$goods->id])->one();
                   if($classGoods == null) {
                       Createclassmanage::create($goods->id);
                   }
                }
                return $this->controller->redirectMessage(\Yii::t('zhtfrontend', '操作成功'), Fun::url([\Yii::$app->controller->id.'/index']));
            }
        }
        return $this->controller->render('@zhtfrontend/components/services/goods/audit.php',['attr'=>$attr,'model'=>$model]);
    }

}